const Koa = require('koa');
const views = require('koa-views');
let staticFiles = require('./static-files');

// 注意require('koa-router')返回的是函数:
const router = require('koa-router')();
const bodyparser = require('koa-bodyparser');
const session = require("koa-session2");
const app = new Koa();

// 导入controller middleware:
const controller = require('./controller');

app.use(staticFiles('/static/', __dirname + '/static'));
app.use(views(__dirname + '/views-ejs', {
    extension: 'ejs'
}));
app.use(session({
    key: "SESSIONID",   //default "koa:sess"
    maxAge: 5000  //设置session超时时间
}));
//这里设置一个session中间件。然后就可以实现全局的回话
app.use(async (ctx, next) => {
    if(ctx.session.view === undefined) { //统计访问次数
        ctx.session.view = 0
    } else {
        ctx.session.view += 1
    }
    ctx.state={
        view:ctx.session.view
    };
    await next();
});
app.use(bodyparser());
// 使用middleware:
app.use(controller());



app.listen(3000);
console.log('app started at port 3000...');